package com.fly.estore.service.Impl;

import com.fly.estore.constant.AccountConstant;
import com.fly.estore.dao.AccountDAO;
import com.fly.estore.domain.AccountInfo;
import com.fly.estore.framework.session.SessionManager;
import com.fly.estore.framework.util.CommonUtil;
import com.fly.estore.framework.util.CookieUtil;
import com.fly.estore.service.AccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Fly on 2016/3/25.
 */
@Service("AccountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    AccountDAO accountDAO;
    @Autowired
    SessionManager sessionManager;

    /**
     * 创建账号
     * @param accountInfo   账号对象
     * @return 更新记录数
     */
    public int create(AccountInfo accountInfo) {
        return accountDAO.create(accountInfo);
    }

    /**
     * 更新账号
     * @param accountInfo   账号对象
     * @return  更新记录数
     */
    public int update(AccountInfo accountInfo) {
        return accountDAO.update(accountInfo);
    }

    public int save(AccountInfo accountInfo) {
        int update = 0;
        if (StringUtils.isBlank(accountInfo.getAccountId())){
            accountInfo.setAccountId(CommonUtil.genPK());
            accountInfo.setPassword(CommonUtil.MD5Encode("123456"));
            update = accountDAO.create(accountInfo);
        }
        else{
            update = accountDAO.update(accountInfo);
        }
        return update;
    }

    /**
     * 根据Id删除账号
     * @param accountId     账号Id
     * @return  更新记录数
     */
    public int deleteById(String accountId) {
        return accountDAO.deleteById(accountId);
    }

    /**
     * 根据Id获取账号对象
     * @param accountId     账号Id
     * @return  账号对象
     */
    public AccountInfo getById(String accountId) {
        return accountDAO.getById(accountId);
    }

    /**
     * 根据账号名和密码查询
     * @param accountName   账号名
     * @param password      密码
     * @return  账号对象
     */
    public AccountInfo getByAccountNameAndPwd(String accountName, String password) {
        AccountInfo accountInfo = accountDAO.getByAccountNameAndPwd(accountName, CommonUtil.MD5Encode(password));
        return accountInfo;
    }

    /**
     * 账号登录
     * @param accountName 账号
     * @param password      密码
     */
    public void signIn(HttpServletRequest request, HttpServletResponse response, String accountName, String password) {
        //判断账号是否存在
        AccountInfo accountInfo = accountDAO.getByAccountNameAndPwd(accountName, CommonUtil.MD5Encode(password));
        if (accountInfo != null){
            //账号存在
            if (accountInfo.getAccountStatus().equals(AccountConstant.ACCOUNT_STATUS_ENABLE)){
                //账号启用，创建用户session
                String sessionId = sessionManager.createAccountSession(request, accountInfo);
                CookieUtil.writeCookie(response, CookieUtil.ACCOUNT_COOKIE, sessionId);
            }
            else{
                //账号停用
            }
        }
        else{
            //账号不存在
        }
    }

    public int changePassword(String oldPassword, String newPassword, String accountId) {
        int update = 0;
        oldPassword = CommonUtil.MD5Encode(oldPassword);
        newPassword = CommonUtil.MD5Encode(newPassword);
        AccountInfo account = accountDAO.getById(accountId);
        if (null != account){
            if (account.getPassword().equals(oldPassword)){
                account.setPassword(newPassword);
                update = accountDAO.update(account);
            }
        }
        return update;
    }

    public List<AccountInfo> getList() {
        return accountDAO.getList();
    }
}
